package com.hemerasolutions.v2trainer.component.list;

import java.util.ArrayList;
import java.util.List;

import javax.faces.event.ActionEvent;

import org.primefaces.event.TabChangeEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hemerasolutions.v2trainer.component.BaseComponent;
import com.hemerasolutions.v2trainer.component.interfaces.detail.ExerciseDetailsCompInterface;
import com.hemerasolutions.v2trainer.component.interfaces.detail.StatisticDetailsCompInterface;
import com.hemerasolutions.v2trainer.component.interfaces.list.IntensityZoneListCompInterface;
import com.hemerasolutions.v2trainer.component.interfaces.list.ModalityIZListCompInterface;
import com.hemerasolutions.v2trainer.component.interfaces.list.StepListCompInterface;
import com.hemerasolutions.v2trainer.component.interfaces.list.StepListLevelsInterface;
import com.hemerasolutions.v2trainer.component.interfaces.search.ExerciseGroupSearchCompInterface;
import com.hemerasolutions.v2trainer.component.interfaces.search.ExerciseSearchCompInterface;
import com.hemerasolutions.v2trainer.component.interfaces.search.TrainingSearchCompInterface;
import com.hemerasolutions.v2trainer.component.interfaces.search.TrainingSystemSearchCompInterface;
import com.hemerasolutions.v2trainer.delegate.interfaces.ExerciseGroupSearchDelegateInterface;
import com.hemerasolutions.v2trainer.delegate.interfaces.ExerciseSearchDelegateInterface;
import com.hemerasolutions.v2trainer.delegate.interfaces.ModalitySearchDelegateInterface;
import com.hemerasolutions.v2trainer.delegate.interfaces.TrainingSearchDelegateInterface;
import com.hemerasolutions.v2trainer.delegate.interfaces.TrainingSystemSearchDelegateInterface;
import com.hemerasolutions.v2trainer.model.exercise.Exercise;
import com.hemerasolutions.v2trainer.model.exerciseGroup.ExerciseGroup;
import com.hemerasolutions.v2trainer.model.exerciseGroup.VersionableExerciseGroup;
import com.hemerasolutions.v2trainer.model.modality.VersionableModality;
import com.hemerasolutions.v2trainer.model.step.Step;
import com.hemerasolutions.v2trainer.model.training.Training;
import com.hemerasolutions.v2trainer.model.training.TrainingStep;

@Component("stepListLevels")
@Scope("prototype")
public class StepListLevelsComp  extends BaseComponent  implements StepListLevelsInterface,ExerciseSearchDelegateInterface,
	ExerciseGroupSearchDelegateInterface, ModalitySearchDelegateInterface,TrainingSystemSearchDelegateInterface,TrainingSearchDelegateInterface {


	private static final long serialVersionUID = 1L;
	
	private List<TrainingStep> stepLevels;
	
	private TrainingStep stepLevel;
	
	private List<StepListCompInterface> stepListHandlers;	
	
	private StepListCompInterface stepListHandler;
	
	private String panelToUpdate;
	
	private ExerciseGroup exerciseGroup;

	@Autowired
	private StatisticDetailsCompInterface statisticHandler;

	@Autowired
	private ExerciseDetailsCompInterface exerciseDetailsComp;

	@Autowired 
	private ExerciseSearchCompInterface exerciseSearchComp;
	
	@Autowired
	private ExerciseGroupSearchCompInterface exerciseGroupSearchComp;

	@Autowired
	private StepListCompInterface egListHandler;
	
	@Autowired
	private ExerciseDetailsCompInterface exerciseDetailsHandler;

	@Autowired
	private StepListCompInterface egPreviewListHandler;
	
	@Autowired
	private TrainingSystemSearchCompInterface tsSearchComp;

	@Autowired
	private TrainingSearchCompInterface trainingSearchComp;	
	
	@Autowired
	private transient ApplicationContext ctx;

	@Autowired
	private IntensityZoneListCompInterface izHandler;
	
	@Autowired
	private ModalityIZListCompInterface imzHandler;
	
	
	private Exercise selExercise;

	private TrainingStep referenceStepContainer;	
	
	private TrainingStep egPreviewStepContainer;	

	public StepListLevelsComp(){
		super();
	}

	@Override
	public void updateCurrentPanel(){
		this.updatePFComponnent(this.panelToUpdate);
	}

	@Override
	public void copyLevel(ActionEvent ev){
		
		TrainingStep otherStepLevel = (TrainingStep)ev.getComponent().getAttributes().get("source");

		this.stepListHandler.getStepLevel().getStatisticUnits().clear();
		this.stepListHandler.getStepLevel().getStatisticUnits().addAll(otherStepLevel.getStatisticUnits());
		this.stepListHandler.clearSteps();
		this.stepListHandler.addSteps(otherStepLevel.getRootStep().getSteps());

	}

	@Override
	public void copyToAll(ActionEvent ev){
		StepListCompInterface otherStepLevel = (StepListCompInterface)ev.getComponent().getAttributes().get("source");

		for(StepListCompInterface stepList:this.stepListHandlers){
			if(otherStepLevel.getLevel() != stepList.getLevel()){

				stepList.getStepLevel().getStatisticUnits().clear();
				stepList.getStepLevel().getStatisticUnits().addAll(otherStepLevel.getStepLevel().getStatisticUnits());
				stepList.clearSteps();
				stepList.addSteps(otherStepLevel.getStepLevel().getRootStep().getSteps());
			}
		}
	}
	
	@Override
	public void toViewExercise(){
		this.exerciseDetailsHandler.setExercise(this.selExercise);
		this.exerciseDetailsHandler.refresh();
	}
	
	@Override
	public void toSearchTrainingSystem(){


		tsSearchComp.setTsSearchDelegate(this);
		tsSearchComp.refresh();
	}
	

	@Override
	public void toSearchTraining(){


		trainingSearchComp.setTrainingSearchDelegate(this);
		trainingSearchComp.refresh();
	}
	
	
	@Override
	public void importStepListener(ActionEvent ev){
		String importType  	= (String)ev.getComponent().getAttributes().get("importType");
		StepListCompInterface handler	= (StepListCompInterface) ev.getComponent().getAttributes().get("stepLevelHandler");
		String sourcePanel	= (String)ev.getComponent().getAttributes().get("panelToUpdate");	
		
		if(importType.equals("A")){
			this.panelToUpdate = sourcePanel;
			this.stepListHandler = handler;
			this.exerciseSearchComp.setExerciseSearchDelegate(this);
			this.exerciseSearchComp.refresh();
		}else if(importType.equals("B")){
			this.panelToUpdate = sourcePanel;
			this.stepListHandler = handler;
			this.exerciseGroupSearchComp.setExerciseGroupSearchDelegate(this);
			this.exerciseGroupSearchComp.refresh();
		}else if(importType.equals("C")){

			this.panelToUpdate = sourcePanel;
			this.stepListHandler = handler;
			this.toSearchTrainingSystem();
		}else if(importType.equals("D")){

			this.panelToUpdate = sourcePanel;
			this.stepListHandler = handler;
			this.toSearchTraining();
		}
		
	}	
	

    
	
	
	@Override
	public void addStep(ActionEvent ev){
		this.stepListHandler = (StepListCompInterface)ev.getComponent().getAttributes().get("stepLevelHandler");
		
		this.stepListHandler.addStep(ev);
    }

	@Override
	public void addOthersGroup(ActionEvent ev){
		this.panelToUpdate = (String)ev.getComponent().getAttributes().get("panelToUpdate");
		this.addStep(ev);
	}

//	@Override
//	public boolean onExerciseGroupAdd(ExerciseGroup exerciseGroup) {
//		
//		this.stepListHandler.addExerciseGroup(exerciseGroup);
//		this.updatePFComponnent(this.panelToUpdate);
//		
//		
//		return false;
//	}
	
	@Override	
	public void addExerciseGroup(){
		this.stepListHandler.addStepGroup(this.exerciseGroup.getName(),this.egPreviewStepContainer.getRootStep().getSteps());
		this.updatePFComponnent(this.panelToUpdate);
		
	}

	@Override
	public void refreshExerciseGroupPreview(){
		egPreviewStepContainer = new TrainingStep();
		
//		for(Step step:referenceStepContainer.getSteps()){
//			for(ExerciseSubGroup esg:this.exerciseGroup.getExerciseSubGroups()){
//				if(esg.getModality().equals(step.getTemplateExercise().getModality())){
//					for(Exercise exercise:esg.getExercises()){
//						Step stepAux = new Step(step,null);
//						stepAux.convertTemplateToExercise(exercise);
//						egPreviewStepContainer.getSteps().add(stepAux);
//					}
//				}
//			}
//		}
		
		egPreviewListHandler.setStepLevel(egPreviewStepContainer);
		egPreviewListHandler.refresh();

	}
	
	@Override
	public void refresh() {

		this.stepListHandlers = new ArrayList<StepListCompInterface>();

		for(TrainingStep stepLevel:this.stepLevels){
		
			StepListCompInterface stepListHandler = ctx.getBean("stepListComp",StepListCompInterface.class); ;
			
			stepListHandler.setImzHandler(imzHandler);
			stepListHandler.setIzHandler(izHandler);
			stepListHandler.setStatisticHandler(statisticHandler);
			stepListHandler.setStepLevel(stepLevel);
			stepListHandler.refresh();
			
			this.stepListHandlers.add(stepListHandler);			
		}		
	}

	@Override
	public boolean onExerciseAdd(Exercise exercise) {
		
		boolean removeFromSet = this.stepListHandler.addExerciseStep(exercise);
		
		this.updatePFComponnent(panelToUpdate);

		return  removeFromSet;
	}
	

	@Override
	public void onTrainingSystemImportSteps(List<Step> steps) {
		this.stepListHandler.addSteps(steps);
		this.updatePFComponnent(this.panelToUpdate);
	}
	
	@Override	
	public void tabExerciseGroupChanged(TabChangeEvent event){
		this.refreshExerciseGroupPreview();
	}
		
	
	@Override
	public List<StepListCompInterface> getStepListHandlers() {
		return stepListHandlers;
	}

	@Override
	public void setStepListHandlers(List<StepListCompInterface> stepListHandlers) {
		this.stepListHandlers = stepListHandlers;
	}

	@Override
	public StepListCompInterface getStepListHandler() {
		return stepListHandler;
	}

	@Override
	public void setStepListHandler(StepListCompInterface stepListHandler) {
		this.stepListHandler = stepListHandler;
	}

	@Override
	public String getPanelToUpdate() {
		return panelToUpdate;
	}

	@Override
	public void setPanelToUpdate(String panelToUpdate) {
		this.panelToUpdate = panelToUpdate;
	}

	@Override
	public ExerciseDetailsCompInterface getExerciseDetailsComp() {
		return exerciseDetailsComp;
	}

	@Override
	public void setExerciseDetailsComp(ExerciseDetailsCompInterface exerciseDetailsComp) {
		this.exerciseDetailsComp = exerciseDetailsComp;
	}

	@Override
	public List<TrainingStep> getStepLevels() {
		return stepLevels;
	}

	@Override
	public void setStepLevels(List<TrainingStep> stepLevels) {
		this.stepLevels = stepLevels;
	}

	@Override
	public TrainingStep getStepLevel() {
		return stepLevel;
	}

	@Override
	public void setStepLevel(TrainingStep stepLevel) {
		this.stepLevel = stepLevel;
	}

	@Override
	public Exercise getSelExercise() {
		return selExercise;
	}

	@Override
	public void setSelExercise(Exercise selExercise) {
		this.selExercise = selExercise;
	}

	@Override
	public ExerciseSearchCompInterface getExerciseSearchComp() {
		return exerciseSearchComp;
	}

	@Override
	public void setExerciseSearchComp(ExerciseSearchCompInterface exerciseSearchComp) {
		this.exerciseSearchComp = exerciseSearchComp;
	}

	@Override
	public ExerciseGroupSearchCompInterface getExerciseGroupSearchComp() {
		return exerciseGroupSearchComp;
	}

	@Override
	public void setExerciseGroupSearchComp(
			ExerciseGroupSearchCompInterface exerciseGroupSearchComp) {
		this.exerciseGroupSearchComp = exerciseGroupSearchComp;
	}

	@Override
	public ExerciseGroup getExerciseGroup() {
		return exerciseGroup;
	}

	@Override
	public void setExerciseGroup(ExerciseGroup exerciseGroup) {
		this.exerciseGroup = exerciseGroup;
	}

	@Override
	public TrainingStep getReferenceStepContainer() {
		return referenceStepContainer;
	}

	@Override
	public void setReferenceStepContainer(TrainingStep referenceStepContainer) {
		this.referenceStepContainer = referenceStepContainer;
	}

	@Override
	public StepListCompInterface getEgListHandler() {
		return egListHandler;
	}

	@Override
	public void setEgListHandler(StepListCompInterface egListHandler) {
		this.egListHandler = egListHandler;
	}

	@Override
	public ExerciseDetailsCompInterface getExerciseDetailsHandler() {
		return exerciseDetailsHandler;
	}

	@Override
	public void setExerciseDetailsHandler(ExerciseDetailsCompInterface exerciseDetailsHandler) {
		this.exerciseDetailsHandler = exerciseDetailsHandler;
	}

	@Override
	public StepListCompInterface getEgPreviewListHandler() {
		return egPreviewListHandler;
	}

	@Override
	public void setEgPreviewListHandler(StepListCompInterface egPreviewListHandler) {
		this.egPreviewListHandler = egPreviewListHandler;
	}

	@Override
	public TrainingStep getEgPreviewStepContainer() {
		return egPreviewStepContainer;
	}

	@Override
	public void setEgPreviewStepContainer(TrainingStep egPreviewStepContainer) {
		this.egPreviewStepContainer = egPreviewStepContainer;
	}

	@Override
	public TrainingSystemSearchCompInterface getTsSearchComp() {
		return tsSearchComp;
	}

	@Override
	public void setTsSearchComp(TrainingSystemSearchCompInterface tsSearchComp) {
		this.tsSearchComp = tsSearchComp;
	}

	@Override
	public boolean onTrainingSystemAdd(Training trainingSystem) {
		return false;
	}

	@Override
	public boolean onTrainingAdd(Training training) {
		return false;
	}

	@Override
	public void onTrainingImportSteps(List<Step> steps) {
		this.stepListHandler.addSteps(steps);
		
		this.updatePFComponnent(this.panelToUpdate);
	}

	@Override
	public TrainingSearchCompInterface getTrainingSearchComp() {
		return trainingSearchComp;
	}

	@Override
	public void setTrainingSearchComp(TrainingSearchCompInterface trainingSearchComp) {
		this.trainingSearchComp = trainingSearchComp;
	}

	@Override
	public void onSearchComplete(List<Training> trainings) {
		
	}

	@Override
	public IntensityZoneListCompInterface getIzHandler() {
		return izHandler;
	}

	@Override
	public void setIzHandler(IntensityZoneListCompInterface izHandler) {
		this.izHandler = izHandler;
	}

	@Override
	public ModalityIZListCompInterface getImzHandler() {
		return imzHandler;
	}

	@Override
	public void setImzHandler(ModalityIZListCompInterface imzHandler) {
		this.imzHandler = imzHandler;
	}

	@Override
	public StatisticDetailsCompInterface getStatisticHandler() {
		return statisticHandler;
	}

	@Override
	public void setStatisticHandler(StatisticDetailsCompInterface statisticHandler) {
		this.statisticHandler = statisticHandler;
	}

	@Override
	public boolean onVersionableModalitySelected(
			VersionableModality versionableModality) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onVersionableExerciseGroupSelected(
			VersionableExerciseGroup VersionableExerciseGroup) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onVersionableModalitySearchCompleted(
			List<VersionableModality> versionableModalitys) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onVersionableExerciseGroupSearchCompleted(
			List<VersionableExerciseGroup> versionableExerciseGroup) {
		// TODO Auto-generated method stub
		
	}


}
